/*
 * JWeekCell.java
 *
 * Created on 22 de agosto de 2007, 8:13
 */

package es.atareao.ferraplan.gui;

import es.atareao.alejandria.gui.ErrorDialog;
import java.sql.SQLException;
import es.atareao.queensboro.db.Condition;
import es.atareao.ferraplan.lib.Forma;
import es.atareao.ferraplan.lib.Grupo;
import java.util.Vector;

/**
 *
 * @author  Propietario
 */
public class JLittleGrupo extends javax.swing.JPanel {

    /** Creates new form JWeekCell */
    public JLittleGrupo() {
        initComponents();
        this.jLittleForma1.setId(1);
        this.jLittleForma2.setId(2);
        this.jLittleForma3.setId(3);
        this.jLittleForma4.setId(4);
    }
    public JLittleForma getJLittleForma(int JLittleForma){
        switch(JLittleForma){
            case 1:
                return this.jLittleForma1;
            case 2:
                return this.jLittleForma2;
            case 3:
                return this.jLittleForma3;
            case 4:
                return this.jLittleForma4;
        }
        return null;
    }
    private void ejecutaEvento(java.beans.PropertyChangeEvent evt, int selectedJLittleForma){
        if(evt.getPropertyName().equals("selectedForma")){
            this.firePropertyChange("selectedFormaInGrupo",null,selectedJLittleForma);
            this._selectedJLittleForma=selectedJLittleForma;
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLittleForma1 = new es.atareao.ferraplan.gui.JLittleForma();
        jLittleForma2 = new es.atareao.ferraplan.gui.JLittleForma();
        jLittleForma3 = new es.atareao.ferraplan.gui.JLittleForma();
        jLittleForma4 = new es.atareao.ferraplan.gui.JLittleForma();

        setLayout(new java.awt.GridLayout(1, 0));

        jLittleForma1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jLittleForma1PropertyChange(evt);
            }
        });
        add(jLittleForma1);

        jLittleForma2.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jLittleForma2PropertyChange(evt);
            }
        });
        add(jLittleForma2);

        jLittleForma3.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jLittleForma3PropertyChange(evt);
            }
        });
        add(jLittleForma3);

        jLittleForma4.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jLittleForma4PropertyChange(evt);
            }
        });
        add(jLittleForma4);
    }// </editor-fold>//GEN-END:initComponents

    private void jLittleForma1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jLittleForma1PropertyChange
        this.ejecutaEvento(evt,1);
    }//GEN-LAST:event_jLittleForma1PropertyChange

    private void jLittleForma2PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jLittleForma2PropertyChange
        this.ejecutaEvento(evt,2);
    }//GEN-LAST:event_jLittleForma2PropertyChange

    private void jLittleForma3PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jLittleForma3PropertyChange
        this.ejecutaEvento(evt,3);
    }//GEN-LAST:event_jLittleForma3PropertyChange

    private void jLittleForma4PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jLittleForma4PropertyChange
        this.ejecutaEvento(evt,4);
    }//GEN-LAST:event_jLittleForma4PropertyChange

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private es.atareao.ferraplan.gui.JLittleForma jLittleForma1;
    private es.atareao.ferraplan.gui.JLittleForma jLittleForma2;
    private es.atareao.ferraplan.gui.JLittleForma jLittleForma3;
    private es.atareao.ferraplan.gui.JLittleForma jLittleForma4;
    // End of variables declaration//GEN-END:variables

    private Grupo _grupo;
    private boolean _selected=false;
    private int _selectedJLittleForma=0;
    private int _id=0;


    public boolean isSelected() {
        return _selected;
    }
    public void setSelected(boolean selected) {
        this._selected = selected;
    }

    public Grupo getGrupo() {
        return _grupo;
    }

    public void setGrupo(Grupo grupo,Vector<Forma> formas) {
        Forma[] aformas=new Forma[4];
        int contador=-1;
        for(Forma selected:formas){
            if(selected.getValue("grupo_id").equals(grupo.getId())){
                contador++;
                aformas[contador]=selected;
            }
        }/*
        for(int rellena=contador+1;rellena<4;rellena++){
            aformas[contador]=null;
        }
          * 
          */
        this.jLittleForma1.setForma(aformas[0]);
        this.jLittleForma2.setForma(aformas[1]);
        this.jLittleForma3.setForma(aformas[2]);
        this.jLittleForma4.setForma(aformas[3]);
        this.jLittleForma1.setSelectedGrupo(this.isSelected());
        this.jLittleForma2.setSelectedGrupo(this.isSelected());
        this.jLittleForma3.setSelectedGrupo(this.isSelected());
        this.jLittleForma4.setSelectedGrupo(this.isSelected());
        this.jLittleForma1.colorea();
        this.jLittleForma2.colorea();
        this.jLittleForma3.colorea();
        this.jLittleForma4.colorea();
        /*
        try {
            this._grupo = grupo;
            Condition condition = new Condition("grupo_id", grupo.getId(), Condition.IGUAL);
            Forma forma = new Forma(grupo.getConector());
            Vector<Forma> formas = forma.find(condition,"orden");
            Forma[] aformas=new Forma[4];
            for(int contador=0;contador<formas.size();contador++){
                aformas[contador]=formas.get(contador);
            }
            for(int contador=formas.size();contador<4;contador++){
                aformas[contador]=null;
            }
            this.jLittleForma1.setForma(aformas[0]);
            this.jLittleForma2.setForma(aformas[1]);
            this.jLittleForma3.setForma(aformas[2]);
            this.jLittleForma4.setForma(aformas[3]);
            this.jLittleForma1.setSelectedGrupo(this.isSelected());
            this.jLittleForma2.setSelectedGrupo(this.isSelected());
            this.jLittleForma3.setSelectedGrupo(this.isSelected());
            this.jLittleForma4.setSelectedGrupo(this.isSelected());
            this.jLittleForma1.colorea();
            this.jLittleForma2.colorea();
            this.jLittleForma3.colorea();
            this.jLittleForma4.colorea();
        } catch (SQLException ex) {
            ErrorDialog.manejaError(ex);
        }
         *
         */
    }

    /**
     * @return the _selectedJLittleForma
     */
    public int getSelectedJLittleForma() {
        return _selectedJLittleForma;
    }

    /**
     * @param selectedJLittleForma the _selectedJLittleForma to set
     */
    public void setSelectedJLittleForma(int selectedJLittleForma) {
        this._selectedJLittleForma = selectedJLittleForma;
    }

    /**
     * @return the _id
     */
    public int getId() {
        return _id;
    }

    /**
     * @param id the _id to set
     */
    public void setId(int id) {
        this._id = id;
    }

}